@$A = -> new ECAPI()
@$S = (param)-> new ECAPI().system(param)
@$P = (param)-> new ECAPI().page(param)
@$V = (param)-> new ECAPI().view(param)

@map = ()-> new ECAPI().map()
@http = ()-> new ECAPI().system("Http")
@zip = ()-> new ECAPI().system("ZipArchive")
@db = ()-> new ECAPI().database()
ECAPI = ->
    @action = {pid: _env.parent_id}
    @

ECAPI:: = {}


# domain
ECAPI::page = (add_action) ->
ECAPI::widget = (add_action) ->
ECAPI::view = (add_action) ->
ECAPI::database = (add_action) ->
ECAPI::app = (add_action) ->
ECAPI::system = (add_action) ->
ECAPI::map = (add_action) ->
ECAPI::media = (add_action) ->
ECAPI::service = (add_action) ->

# system
ECAPI::callNativeMethod = (call_with_params) -> #ios
ECAPI::unCompress = (call_with_params) ->
ECAPI::compress = (call_with_params) ->
ECAPI::loadFile = (call_with_params) ->

ECAPI::showImageGallery = (call_with_params) ->

# app
ECAPI::getCache = (call_with_params) ->
ECAPI::getCaches = (call_with_params) ->
ECAPI::putCache = (call_with_params) ->

ECAPI::confirm = (call_with_params) ->
ECAPI::makeToast = (call_with_params) ->

# http 请求
ECAPI::get = (call_with_params) ->
ECAPI::post = (call_with_params) ->
ECAPI::download = (call_with_params) ->

# page
ECAPI::onCreated = (call_with_params) ->
ECAPI::onResume = (call_with_params) ->
ECAPI::param = (call_with_params) ->
ECAPI::open = (call_with_params) ->
ECAPI::showActivity = (call_with_params) ->

#view
ECAPI::attr = (call_with_params) ->
ECAPI::onClick = (call_with_params) ->
ECAPI::insert = (call_with_params) ->
ECAPI::badge = (call_with_params) ->
ECAPI::popMenu = (call_with_params) ->
ECAPI::removeSubview = (call_with_params) ->#清空subviews
ECAPI::addSubview = (call_with_params) ->

#widget
ECAPI::onItemClick = (call_with_params) ->

# map
ECAPI::locate = (call_with_params) ->
ECAPI::distanceTo = (call_with_params) ->
ECAPI::distance = (call_with_params) ->

# db
ECAPI::put = (call_with_params) ->
ECAPI::get = (call_with_params) ->
ECAPI::delete = (call_with_params) ->
ECAPI::query = (call_with_params) ->

# media
ECAPI::scanQRCode = (call_with_params) ->
  
# pushService
ECAPI::onRecieved = (call_with_params) ->

#utiliy method
ECAPI::compileTpl = (call_with_params) ->

(->
    deviceCallPromise = (self, action, query) ->
        new Promise (resolve, reject) ->
            self.action[__pid] = _env.pid;
            callCoreApi action, query, resolve, self.action

    addParams = (self, action, query) ->
        self.action[action] = query or null
        self.action[__api] = action
        self

    makeAPI = (orig_api) ->
        add_action_fn = ((add_action) ->).toString()
        call_with_params_fn = ((call_with_params) ->).toString()

        orig_api = orig_api.prototype
        for key of orig_api
            fn_str = orig_api[key].toString()
            if fn_str == add_action_fn
                orig_api[key] = ((key) -> (query) -> addParams @, key, query) key
            else if fn_str == call_with_params_fn
                orig_api[key] = ((key) -> (query) -> deviceCallPromise @, key, query) key

        null

    makeAPI(ECAPI)) null
